/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.p.narcissus.ui.components;

import java.awt.Component;
import java.security.InvalidParameterException;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import com.google.code.p.narcissus.core.model.ComparisonResult.PictureComparisonResult;
import com.google.code.p.narcissus.ui.TestRun;

/**
 *
 * @author jadoj
 */
public class ComparisonTreeCellRenderer extends DefaultTreeCellRenderer {

    private static final String TEST_OK_ICON_PATH = "/com/google/code/p/narcissus/ui/icons/test-ok.png";
    private static final String TEST_ERROR_ICON_PATH = "/com/google/code/p/narcissus/ui/icons/test-error.png";
    private static final String TEST_UNKNOWN_ICON_PATH = "/com/google/code/p/narcissus/ui/icons/test-unknown.png";
    private static ImageIcon okIcon, errorIcon, unknownIcon;

    private boolean isFailingTest(Object treeNode) {
        DefaultMutableTreeNode theTreeNode = (DefaultMutableTreeNode) treeNode;
        if(theTreeNode.getUserObject() instanceof TestRun){
            return (PictureComparisonResult.verificationFail.equals(((TestRun)theTreeNode.getUserObject()).getComparison().getResult()));
        }else {
            throw new InvalidParameterException("Not yet implemented");
        }
    }

    private boolean isWorkingTest(Object treeNode) {
        DefaultMutableTreeNode theTreeNode = (DefaultMutableTreeNode) treeNode;
        if(theTreeNode.getUserObject() instanceof TestRun){
            return (PictureComparisonResult.verificationSuccess.equals(((TestRun)theTreeNode.getUserObject()).getComparison().getResult()));
        }else throw new InvalidParameterException("Not yet implemented");
    }

    private boolean isTestRun(Object TreeNode) {
        return ((DefaultMutableTreeNode)TreeNode).getUserObject() instanceof TestRun;
    }

    private enum TestIcon {
        ok,
        error,
        unknown
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        if (leaf && isTestRun(value)) {
            if (isFailingTest(value)) {
                setIcon(getIcon(TestIcon.error));
                setToolTipText("Test failed");
            } else if (isWorkingTest(value)) {
                setIcon(getIcon(TestIcon.ok));
                setToolTipText("Test passed");
            } else {
                // unknown
                setIcon(getIcon(TestIcon.unknown));
                setToolTipText("Unknown test result!");
            }
        } else {
            setToolTipText(null); //no tool tip
        }

        return this;
    }

    private ImageIcon getIcon(TestIcon icon) {
        switch (icon) {
            case error:
                if (errorIcon == null) {
                    errorIcon = new ImageIcon(ComparisonTreeCellRenderer.class.getResource(getIconPath(icon)));
                }
                return errorIcon;
            case ok:
                if (okIcon == null) {
                    okIcon = new ImageIcon(ComparisonTreeCellRenderer.class.getResource(getIconPath(icon)));
                }
                return okIcon;
            default:
                if (unknownIcon == null) {
                    unknownIcon = new ImageIcon(ComparisonTreeCellRenderer.class.getResource(getIconPath(icon)));
                }
                return unknownIcon;
        }
    }

    private String getIconPath(TestIcon icon) {
        return TestIcon.error.equals(icon) ? TEST_ERROR_ICON_PATH : (TestIcon.ok.equals(icon) ? TEST_OK_ICON_PATH : TEST_UNKNOWN_ICON_PATH);
    }
}
